package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import com.histonepos.npsdk.api.IServiceManager;
import com.histonepos.npsdk.bind.Const;
import com.histonepos.npsdk.bind.IServiceConnector;
import com.histonepos.npsdk.bind.ScaleConst;
import com.histonepos.npsdk.bind.SmartService;
import com.histonepos.npsdk.scale.IScaleEventListener;
import com.histonepos.npsdk.scale.IScaleService;
import com.zbw.weightlibrary.Connector.BaseControlImpl;
import com.zbw.weightlibrary.Connector.IinfoCallback;

/**
 * 作者 ：Wesley
 * 时间 ：2025-04-03 10:17
 * 这个类是干嘛的？：Histone
 * 海信
 */
public class Histone extends BaseControlImpl {

    private final Context mContext;
    private final IinfoCallback mCallback;
    private static boolean isGetWeight = false;
    private IScaleService service;

    public Histone(Context context, IinfoCallback callback) {
        super(callback);
        mContext = context;
        mCallback = callback;
    }

    private void setConfig() {
        final Bundle extra = new Bundle();
        extra.putInt(ScaleConst.TYPE,3);//固定传此参即可
        try {
            if(service != null) {
                service.config(extra);
            }
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void bleConnect() {
        SmartService.getInstance().register(mContext, new IServiceConnector() {
            @Override
            public void onServiceConnected(IServiceManager iServiceManager) {
                try {
                    Log.d("SDKVersion", "SDKVersion: "+iServiceManager.getSDKVersion());
                    service = IScaleService.Stub.asInterface(iServiceManager.getService(Const.SCALE));
                    setConfig();
                    if(service != null) {
                        boolean isOpen = false;
                        try {
                            isOpen = service.open("/dev/ttyS3|9600|0");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if(isOpen) {
                            mCallback.onConnect("连接成功！", true);
                            weightCallback();
                        }else {
                            mCallback.onConnect("连接失败！", false);
                        }
                    }else {
                        mCallback.onConnect("连接失败！", false);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected() {
                service = null;
            }
        });
    }

    private void weightCallback() {
        try {
            service.getWeight(new IScaleEventListener.Stub() {
                @Override
                public void dataMessage(String s) throws RemoteException {

                }

                @Override
                public void weightChange(String s, String s1, String s2, String s3, String s4) throws RemoteException {

                }

                @Override
                public void onWeightBack(String status, float netWeight, float tareWeight, String unit) throws RemoteException {
                    Log.d("onWeightBack", "onWeightBack: "+status);
                    if(isGetWeight){
                        if("g".equals(unit)){
                            mCallback.onGetWeight(decimalFormat.format(netWeight / 1000), "ST".equals(status));
                        }else if("kg".equals(unit)){
                            mCallback.onGetWeight(decimalFormat.format(netWeight), "ST".equals(status));
                        }
                    }
                }
            });
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void bleDisconnect() {
        try {
            if(service != null) {
                service.close();
            }
            SmartService.getInstance().unregister();
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
        mCallback.onDisconnect("断开设备",true);
    }

    @Override
    public void getWeight() {
        isGetWeight = true;
    }

    @Override
    public void stopWeight() {
        isGetWeight = false;
    }

    @Override
    public void clearTare() {
        try {
            if(service != null) {
                service.exitTareWeight();
            }
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void tare() {
        try {
            if(service != null) {
                service.setTareWeight();
            }
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void zero() {
        try {
            if(service != null) {
                service.setZeroWeight();
            }
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }
}
